/*
 * ThemHocSinhInternalFrame.java
 *
 * Created on Jul 15, 2010, 10:35:18 PM
 */
package GUI;

import BUS.HocSinhBUS;
import POJO.HocSinhPOJO;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author MinhHieu
 */
public class HocSinhInternalFrame extends javax.swing.JInternalFrame {

    private boolean TrangThai;

    public void setYearForComboBoxs(){
        DefaultComboBoxModel nm = new DefaultComboBoxModel();
        //ngay he thong
        Date d = new Date(System.currentTimeMillis());
        SimpleDateFormat dayFormat= new SimpleDateFormat("yyyy");
        int thisyear = Integer.parseInt(dayFormat.format(d.getTime()));
        for(int i=thisyear-30;i<=thisyear;i++){
            nm.addElement(String.valueOf(i));
        }
        this.jCBNam.setModel(nm);
    }

    public void LoadLaiBang() {
        try {
            ArrayList<HocSinhPOJO> hs = HocSinhBUS.layhocsinh();
            DefaultTableModel dt = new DefaultTableModel() {
                @Override
                // khong co edit table
                public boolean isCellEditable(int row, int column) {
                    return false;
                }
            };
            dt.addColumn("STT");
            dt.addColumn("Mã học sinh");
            dt.addColumn("Họ và Tên");
            dt.addColumn("Ngày sinh");
            dt.addColumn("Giới tính");
            dt.addColumn("Địa chỉ");
            dt.addColumn("Email");
            // doc du lieu
            dt.setRowCount(hs.size());
            for (int i = 0; i < hs.size(); i++) {
                dt.setValueAt(i + 1, i, 0);
                dt.setValueAt(hs.get(i).getMaHS(), i, 1);
                dt.setValueAt(hs.get(i).getTenHS(), i, 2);
                String[] Temp = hs.get(i).getNgaySinh().split("-");
                String NgaySinh = String.format("%s-%s-%s", Temp[2],Temp[1],Temp[0]);
                dt.setValueAt(NgaySinh, i, 3);
                dt.setValueAt(hs.get(i).getGioiTinh(), i, 4);
                dt.setValueAt(hs.get(i).getDiaChi(), i, 5);
                dt.setValueAt(hs.get(i).getEmail(), i, 6);
            }
            //gan mode cho table
            this.jTDSHS.setModel(dt);
            this.jTDSHS.changeSelection(0, 0, false, false);
            this.jLThongtin.setText("Học sinh đang được chọn");
            this.setInfo();
//            this.jSpinner1.setVisible(false);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }

    }
    // Ham lay du lieu tu ban len cho cac texttfied
    public void setInfo() {
        int row = this.jTDSHS.getSelectedRow();
        if(row>=0){
            this.jTFMaHS.setText(this.jTDSHS.getValueAt(row, 1).toString());
            this.jTFHoten.setText(this.jTDSHS.getValueAt(row, 2).toString());
            String ntn = this.jTDSHS.getValueAt(row, 3).toString();
            String[] m = ntn.split("-");
            this.jCBNam.setSelectedItem(m[2]);
            this.jCBThang.setSelectedItem(m[1]);
            this.jCBNgay.setSelectedItem(m[0]);
            this.jCBGTinh.setSelectedItem(this.jTDSHS.getValueAt(row, 4).toString());
            this.jTFDiaChi.setText(this.jTDSHS.getValueAt(row, 5).toString());
            this.jTFEmail.setText((String)this.jTDSHS.getValueAt(row, 6));
        }
    }
    // tat cac button
    public void setButtonOff() {
        this.jBTLuu.setEnabled(true);
        this.jBHuyBo.setEnabled(true);
        this.jBThem.setEnabled(false);
        this.jBXoa.setEnabled(false);
        this.jBCapNhat.setEnabled(false);
    }
    // mo cac button
    public void setButtonOn() {
        this.jBTLuu.setEnabled(false);
        this.jBHuyBo.setEnabled(false);
        this.jBThem.setEnabled(true);
        this.jBXoa.setEnabled(true);
        this.jBCapNhat.setEnabled(true);
    }

    /** Creates new form ThemHocSinhInternalFrame */
    public HocSinhInternalFrame() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jBThoat = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel7 = new javax.swing.JLabel();
        jTFMaHS = new javax.swing.JTextField();
        jTFHoten = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTFEmail = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jCBGTinh = new javax.swing.JComboBox();
        jTFDiaChi = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jBTLuu = new javax.swing.JButton();
        jLThongtin = new javax.swing.JLabel();
        jBHuyBo = new javax.swing.JButton();
        jCBNgay = new javax.swing.JComboBox();
        jCBThang = new javax.swing.JComboBox();
        jCBNam = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jBXoa = new javax.swing.JButton();
        jBCapNhat = new javax.swing.JButton();
        jBRefesh = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTDSHS = new javax.swing.JTable();
        jBThem = new javax.swing.JButton();

        setClosable(true);
        setIconifiable(true);
        setTitle("Quản lý học sinh - Tiếp nhận học sinh");
        setMinimumSize(new java.awt.Dimension(675, 480));
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameOpened(evt);
            }
        });

        jLabel1.setBackground(new java.awt.Color(255, 51, 255));
        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 24));
        jLabel1.setForeground(new java.awt.Color(0, 102, 255));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Tiếp nhận học Sinh");

        jBThoat.setText("Thoát");
        jBThoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBThoatActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, " Thông tin", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(51, 51, 255))); // NOI18N

        jLabel7.setText("Mã học sinh:");

        jTFMaHS.setEnabled(false);

        jTFHoten.setEnabled(false);

        jLabel3.setText("Ngày sinh:");

        jTFEmail.setEnabled(false);

        jLabel4.setText("Email:");

        jLabel5.setText("Giới tính:");

        jCBGTinh.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Nam", "Nữ" }));
        jCBGTinh.setEnabled(false);

        jTFDiaChi.setEnabled(false);

        jLabel6.setText("Địa chỉ:");

        jBTLuu.setText("Lưu");
        jBTLuu.setEnabled(false);
        jBTLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBTLuuActionPerformed(evt);
            }
        });

        jLThongtin.setText("Học sinh đang được chọn:");

        jBHuyBo.setText("Hủy bỏ");
        jBHuyBo.setEnabled(false);
        jBHuyBo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBHuyBoActionPerformed(evt);
            }
        });

        jCBNgay.setToolTipText("Ngày");
        jCBNgay.setEnabled(false);

        jCBThang.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12" }));
        jCBThang.setToolTipText("Tháng");
        jCBThang.setEnabled(false);
        jCBThang.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jCBThangItemStateChanged(evt);
            }
        });

        jCBNam.setToolTipText("Năm");
        jCBNam.setEnabled(false);

        jLabel2.setText("Họ và tên:");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLThongtin)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel7)
                            .addComponent(jLabel3)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                                        .addComponent(jCBNgay, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jCBThang, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jCBNam, 0, 65, Short.MAX_VALUE))
                                    .addComponent(jTFHoten, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGap(18, 18, 18))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jTFMaHS, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 50, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jCBGTinh, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel6)
                                    .addComponent(jLabel4))
                                .addGap(18, 18, 18)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(jTFEmail, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE)
                                    .addComponent(jTFDiaChi, javax.swing.GroupLayout.DEFAULT_SIZE, 177, Short.MAX_VALUE))))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jBHuyBo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBTLuu, javax.swing.GroupLayout.PREFERRED_SIZE, 76, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel5)
                                    .addComponent(jCBGTinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel6)
                                    .addComponent(jTFDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 15, Short.MAX_VALUE)
                                    .addComponent(jTFEmail, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jBTLuu)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jBHuyBo)
                                .addGap(16, 16, 16))))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                        .addComponent(jLThongtin)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 12, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTFMaHS, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jTFHoten, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel3)
                            .addComponent(jCBNgay, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCBNam, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jCBThang, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        jBXoa.setText("Xóa");
        jBXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBXoaActionPerformed(evt);
            }
        });

        jBCapNhat.setText("Cập nhật");
        jBCapNhat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBCapNhatActionPerformed(evt);
            }
        });

        jBRefesh.setText("Làm mới");
        jBRefesh.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBRefeshActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Danh sách", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 0, 11), new java.awt.Color(0, 0, 255))); // NOI18N

        jTDSHS.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {},
                {},
                {},
                {}
            },
            new String [] {

            }
        ));
        jTDSHS.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTDSHSMouseClicked(evt);
            }
        });
        jTDSHS.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                jTDSHSKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(jTDSHS);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 490, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 257, Short.MAX_VALUE)
                .addContainerGap())
        );

        jBThem.setText("Thêm");
        jBThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBThemActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.PREFERRED_SIZE, 312, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.CENTER, javax.swing.GroupLayout.DEFAULT_SIZE, 622, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jBCapNhat, 0, 0, Short.MAX_VALUE)
                            .addComponent(jBXoa, javax.swing.GroupLayout.DEFAULT_SIZE, 85, Short.MAX_VALUE)
                            .addComponent(jBThem, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(jBRefesh, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(jBThoat, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 9, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 138, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(31, 31, 31)
                        .addComponent(jBThem, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBXoa, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBCapNhat, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 99, Short.MAX_VALUE)
                        .addComponent(jBRefesh, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jBThoat, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(23, 23, 23)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jBThoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBThoatActionPerformed
        // TODO add your handling code here:
        int kq = JOptionPane.showConfirmDialog(this, "Bạn có muốn thoát không?", "Thông báo", JOptionPane.YES_NO_OPTION);
        if (kq == 0) {
            this.dispose();
        }
    }//GEN-LAST:event_jBThoatActionPerformed

    private void jBTLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBTLuuActionPerformed
        // TODO add your handling code here:
        boolean kq = false;
        String error_message = "";
        boolean error = false;

        String MaHS = this.jTFMaHS.getText();
        String TenHS = this.jTFHoten.getText();
        String GioiTinh = this.jCBGTinh.getSelectedItem().toString();
        String DiaChi = this.jTFDiaChi.getText();
        String Email = this.jTFEmail.getText();
        String ng = this.jCBNgay.getSelectedItem().toString();
        String th = this.jCBThang.getSelectedItem().toString();
        String nm = this.jCBNam.getSelectedItem().toString();
        String NgaySinhsql = String.format("%s-%s-%s", nm,th,ng);
        String NgaySinh = String.format("%s-%s-%s", ng,th,nm);
        
        if (this.TrangThai) {
            for(int i=0;i<this.jTDSHS.getRowCount();i++){
                if(this.jTFMaHS.getText().compareToIgnoreCase(this.jTDSHS.getValueAt(i, 1).toString())==0){
                    error = true;
                    error_message += "Mã học sinh đã tồn tại.\n";
                    this.jTFMaHS.requestFocus();
                    this.jTFMaHS.selectAll();
                    break;
                }
            }
            if (MaHS.isEmpty()) {
                error_message += "Mã học sinh không được để trống.\n";
                error = true;
            }
            if (TenHS.isEmpty()) {
                error_message += "Tên học sinh không được để trống.\n";
                error = true;
            }
            if (DiaChi.isEmpty()) {
                error_message += "Địa chỉ không được để trống.\n";
                error = true;
            }
            if(error){
                JOptionPane.showMessageDialog(this, error_message, "Thông báo", JOptionPane.ERROR_MESSAGE);
            }else{
                kq = HocSinhBUS.themHS(MaHS, TenHS, NgaySinhsql, GioiTinh, DiaChi, Email);
                if (kq) {
                    JOptionPane.showMessageDialog(this, "Thêm thành công.");
                } else {
                    JOptionPane.showMessageDialog(this, "Thêm thất bại");
                }
                this.setButtonOn();
                this.jTFMaHS.setEnabled(false);
                this.jTFHoten.setEnabled(false);
                this.jTFDiaChi.setEnabled(false);
                this.jTFEmail.setEnabled(false);
                this.jCBNgay.setEnabled(false);
                this.jCBThang.setEnabled(false);
                this.jCBNam.setEnabled(false);
                this.jCBGTinh.setEnabled(false);
                this.LoadLaiBang();
            }
        }else{
            int row = this.jTDSHS.getSelectedRow();
            if(TenHS.compareTo(this.jTDSHS.getValueAt(row, 2).toString())==0
              && DiaChi.compareTo(this.jTDSHS.getValueAt(row, 5).toString())==0
              && Email.compareTo(this.jTDSHS.getValueAt(row, 6).toString())==0
              && NgaySinh.compareTo(this.jTDSHS.getValueAt(row, 3).toString())==0
              && GioiTinh.compareTo(this.jTDSHS.getValueAt(row, 4).toString())==0){
                JOptionPane.showMessageDialog(this, "Không có sự thay đổi", "Thông báo", JOptionPane.WARNING_MESSAGE);
            }else {
                kq = HocSinhBUS.suaHS(MaHS, TenHS,NgaySinhsql,GioiTinh, DiaChi, Email);
                if (kq) {
                    JOptionPane.showMessageDialog(this, "Cập nhật thành công.");
                } else {
                    JOptionPane.showMessageDialog(this, "cập nhật thất bại");
                }
            }
            this.setButtonOn();
            this.jTFMaHS.setEnabled(false);
            this.jTFHoten.setEnabled(false);
            this.jTFDiaChi.setEnabled(false);
            this.jTFEmail.setEnabled(false);
            this.jCBNgay.setEnabled(false);
            this.jCBThang.setEnabled(false);
            this.jCBNam.setEnabled(false);
            this.jCBGTinh.setEnabled(false);
            this.LoadLaiBang();
        }
    }//GEN-LAST:event_jBTLuuActionPerformed

    private void formInternalFrameOpened(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameOpened
        // TODO add your handling code here:
        this.setYearForComboBoxs();
        this.LoadLaiBang();
    }//GEN-LAST:event_formInternalFrameOpened

    private void jBXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBXoaActionPerformed
        // TODO add your handling code here:
        int xacnhan = JOptionPane.showConfirmDialog(this, "Bạn thực sự muốn xóa học sinh '" + this.jTFHoten.getText() + "' ?", "Xác nhận", JOptionPane.YES_NO_OPTION);
        if (xacnhan == 0) {
            boolean kq = HocSinhBUS.xoaHS(this.jTFMaHS.getText());
            if (kq == false) {
                JOptionPane.showMessageDialog(this, "Xóa thất bại!");
            }
            this.LoadLaiBang();
        }
    }//GEN-LAST:event_jBXoaActionPerformed

    private void jBCapNhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCapNhatActionPerformed
        // TODO add your handling code here:
        this.jLThongtin.setText("Cập nhật thông tin học sinh");
        this.jTFHoten.setEnabled(true);
        //ngay thang nam sinh
        this.jCBNgay.setEnabled(true);
        this.jCBThang.setEnabled(true);
        this.jCBNam.setEnabled(true);
        this.jCBGTinh.setEnabled(true);
        this.jTFDiaChi.setEnabled(true);
        this.jTFEmail.setEnabled(true);
        this.jTFHoten.requestFocus();
        this.jTFHoten.selectAll();

        this.setButtonOff();

        this.TrangThai = false;
    }//GEN-LAST:event_jBCapNhatActionPerformed

    private void jBRefeshActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBRefeshActionPerformed
        // TODO add your handling code here:
        this.LoadLaiBang();
    }//GEN-LAST:event_jBRefeshActionPerformed

    private void jBThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBThemActionPerformed

        this.jLThongtin.setText("Tiếp nhận học sinh mới");
        this.jTFMaHS.setEnabled(true);
        this.jTFMaHS.setText(null);
        this.jTFHoten.setEnabled(true);
        this.jTFHoten.setText(null);
        this.jCBNgay.setEnabled(true);
        this.jCBThang.setEnabled(true);
        this.jCBNam.setEnabled(true);
        this.jCBGTinh.setEnabled(true);
        this.jTFDiaChi.setEnabled(true);
        this.jTFDiaChi.setText(null);
        this.jTFEmail.setEnabled(true);
        this.jTFEmail.setText(null);
        this.jTFMaHS.requestFocus();

        this.setButtonOff();

        this.TrangThai = true;
    }//GEN-LAST:event_jBThemActionPerformed

    private void jBHuyBoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBHuyBoActionPerformed
        // TODO add your handling code here:
        this.setButtonOn();
        this.jTFMaHS.setEnabled(false);
        this.jTFHoten.setEnabled(false);
        this.jCBNgay.setEnabled(false);
        this.jCBThang.setEnabled(false);
        this.jCBNam.setEnabled(false);
        this.jTFDiaChi.setEnabled(false);
        this.jTFEmail.setEnabled(false);
        this.jCBGTinh.setEnabled(false);
        this.LoadLaiBang();
    }//GEN-LAST:event_jBHuyBoActionPerformed

    private void jTDSHSMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTDSHSMouseClicked
        // TODO add your handling code here:
        this.setInfo();
    }//GEN-LAST:event_jTDSHSMouseClicked

    private void jTDSHSKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jTDSHSKeyReleased
        // TODO add your handling code here:
        if(evt.getKeyCode()==38 || evt.getKeyCode()==40)  this.setInfo();
        if(evt.getKeyCode()==127)  this.jBXoaActionPerformed(null);
    }//GEN-LAST:event_jTDSHSKeyReleased

    private void jCBThangItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jCBThangItemStateChanged
        // TODO add your handling code here:
        DefaultComboBoxModel ngay = new DefaultComboBoxModel();
        String tg = this.jCBThang.getSelectedItem().toString();
        if(tg.equals("02")){
            for(int i=1;i<=29;i++){
                String temp = String.valueOf(i);
                if(i<10){
                    temp = String.format("0%s",i);
                }
                 ngay.addElement(temp);
            }

        }
        else if(tg.equals("04") || tg.equals("06") || tg.equals("09") || tg.equals("11")){
            for(int i=1;i<=30;i++){
                String temp = String.valueOf(i);
                if(i<10){
                    temp = String.format("0%s",i);
                }
                ngay.addElement(temp);
            }
        }
        else{
            for(int i=1;i<=31;i++){
                String temp = String.valueOf(i);
                if(i<10){
                   temp = String.format("0%s",i);
                }
                ngay.addElement(temp);
            }
        }
         this.jCBNgay.setModel(ngay);
    }//GEN-LAST:event_jCBThangItemStateChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCapNhat;
    private javax.swing.JButton jBHuyBo;
    private javax.swing.JButton jBRefesh;
    private javax.swing.JButton jBTLuu;
    private javax.swing.JButton jBThem;
    private javax.swing.JButton jBThoat;
    private javax.swing.JButton jBXoa;
    private javax.swing.JComboBox jCBGTinh;
    private javax.swing.JComboBox jCBNam;
    private javax.swing.JComboBox jCBNgay;
    private javax.swing.JComboBox jCBThang;
    private javax.swing.JLabel jLThongtin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable jTDSHS;
    private javax.swing.JTextField jTFDiaChi;
    private javax.swing.JTextField jTFEmail;
    private javax.swing.JTextField jTFHoten;
    private javax.swing.JTextField jTFMaHS;
    // End of variables declaration//GEN-END:variables
}
